package cn.com.qtgl.sso.oidc.comp.service;

import cn.com.qtgl.apis.builtin.comp.service.BuiltinService;
import cn.com.qtgl.apis.builtin.model.vo.user.User;
import cn.com.qtgl.sso.oidc.model.vo.TokenMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@CacheConfig(cacheNames = {"CurrentUser"})
@Component
public class ProfileService {

    @Resource
    @Lazy
    private TokenService tokenService;

    @Resource
    private BuiltinService builtinService;

    public User getCurrentUser() throws Exception{
        TokenMessage tokenMessage=tokenService.introspect();
        return builtinService.getUser(tokenMessage.getOpenId());
    }

//    @Cacheable(value = "CurrentUser", key = "#token")
    public User getUserByToken(String token) throws Exception{
        TokenMessage tokenMessage=tokenService.introspect(token);
        return builtinService.getUser(tokenMessage.getOpenId());
    }
}
